/*
 * @Author: szx
 * @Date: 2021-12-21 16:31:51
 * @LastEditTime: 2021-12-21 16:47:55
 * @Description:
 * @FilePath: \leetcode\200-299\222\222.js
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var countNodes = function (root) {
    if (!root) return 0;
    const que = [];
    let res = 0;
    que.push(root);
    while (que.length) {
        let length = que.length;
        for (let i = 0; i < length; i++) {
            let node = que.pop();
            res++;
            if (node.left) que.push(node.left);
            if (node.right) que.push(node.right);
        }
    }
    return res;
};
var countNodes = function (root) {
    if (!root) return 0;
    return 1 + countNodes(root.left) + countNodes(root.right);
};
var countNodes = function (root) {
    if (!root) return 0;
    let left = root.left;
    let right = root.right;
    let leftHeight = 0,
        rightHeight = 0;
    while (left) {
        left = left.left;
        leftHeight++;
    }
    while (right) {
        right = right.right;
        rightHeight++;
    }
    if (leftHeight == rightHeight) {
        return Math.pow(2, leftHeight + 1) - 1;
    }
    return 1 + countNodes(root.left) + countNodes(root.right);
};
